package cn.autumnorange.app.util.provider.service;

import cn.autumnorange.app.common.dto.ResultDto;
import cn.autumnorange.app.common.provider.cacheannotation.RedisCacheTarget;
import cn.autumnorange.app.common.provider.queue.Order;
//import cn.autumnorange.app.common.provider.queue.RabbitSender;
import cn.autumnorange.app.util.api.TimeService;
import cn.autumnorange.app.util.api.dto.OrderDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class TimeServiceImpl implements TimeService {
    @RedisCacheTarget(cacheType=RedisCacheTarget.STRINGCACHE,fieldKeys="timestamp")
    @Override
    public ResponseEntity<ResultDto<String>> timestampToYYMMDD(long timestamp) {
        ResultDto resultDto= ResultDto.createMessageResult( ""+timestamp+666);
        return ResponseEntity.ok(resultDto);
    }

    @RedisCacheTarget(cacheType=RedisCacheTarget.STRINGCACHE,fieldKeys="className")
    @Override
    public ResponseEntity<ResultDto<String>> getAnnotation(String className, String methodName, String annotationName) {
        ResultDto resultDto= ResultDto.createDataResult( ""+className+methodName+annotationName);
        return ResponseEntity.ok(resultDto);
    }

    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
//    @Autowired
//    private RabbitSender rabbitSender;
    /** 业务交换机标识符 */
    public static final String BUSINESS_EXCHANGE_NAME = "dead-letter-business-exchange";
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /** 路由交换机标识符 */
    public static final String BUSINESS_EXCHANGE_Routing_NAME = "dead-letter-business-routing-exchange";
    /** 业务路由A队列 */
    public static final String BUSINESS_QUEUEA_Routing_NAME = "dead-letter-business-routing-queuea";
    public static final String topicroutingKey = "orderAAAA";
    @RedisCacheTarget(cacheType=RedisCacheTarget.STRINGCACHE,fieldKeys="childrenDto#childrenDto#idc",cacheTime = 120000)
    @Override
    public ResponseEntity<ResultDto<OrderDto>> updOrder(OrderDto orderDto) throws Exception {
        Map<String, Object> properties = new HashMap<>();
        properties.put("number", orderDto.getId());
        properties.put("send_time", simpleDateFormat.format(new Date()));
        //交换机名称 路由key路由模式需要 发布订阅模式不需要
        rabbitTemplate.convertSendAndReceive(BUSINESS_EXCHANGE_NAME, "", orderDto.getId());
        //路由模式
        rabbitTemplate.convertSendAndReceive("routingexchange", "routingqueuekeya", "路由:"+orderDto.getId());
        rabbitTemplate.convertSendAndReceive("routingexchange","routingqueueBkey" , "路由:"+orderDto.getId());
        //主题模式
//        rabbitTemplate.convertSendAndReceive("topic_dead", "www.wsf.com", "主题路由:"+orderDto.getId());
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "message: M A N ";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
//topicExchange交换机中队列路由键topic.man 和topic.#  发送消息时传入"topic.man" topic.# 队列符合规则
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);

         messageId = String.valueOf(UUID.randomUUID());
         messageData = "message: woman is all ";
         createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);

//        rabbitSender.send("Hello RabbitMQ For Spring Boot!", properties);
//        Order order=new Order();
//        order.setId("1111111111");
//        order.setName("wsf");
//
//        rabbitSender.sendOrder(order);
        ResultDto resultDto= ResultDto.createDataResult(orderDto);

        return ResponseEntity.ok(resultDto);
    }
}
